package com.study.recorderbutton.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.study.recorderbutton.R;

/**
 * 对话框管理工具类
 * @author Administrator
 *
 */
public class DialogManager {
	private Dialog mDialog; //对话框
	
	private ImageView mIcon; //录音图标
	private ImageView mVoice; //音量显示
	private TextView mLabel; //提示文字
	
	private Context mContext; //上下文对象
	
	public DialogManager(Context context){
		mContext = context;
	}
	
	/**
	 * 显示对话框
	 */
	@SuppressLint("InflateParams")
	public void showRecordingDialog(){
		mDialog = new Dialog(mContext, R.style.Theme_AudioDialog);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View view = inflater.inflate(R.layout.dialog_recorder, null);
		mDialog.setContentView(view);
		
		mIcon = (ImageView) mDialog.findViewById(R.id.id_recorder_dialog_icon);
		mVoice = (ImageView) mDialog.findViewById(R.id.id_recorder_dialog_voice);
		mLabel = (TextView) mDialog.findViewById(R.id.id_recorder_dialog_label);
		
		mDialog.show();
	}
	
	/**
	 * 正在录音状态的对话框
	 */
	public void recording(){
		if(mDialog != null && mDialog.isShowing()){
			mIcon.setVisibility(View.VISIBLE);
			mVoice.setVisibility(View.VISIBLE);
			mLabel.setVisibility(View.VISIBLE);
			
			mIcon.setImageResource(R.drawable.recorder);
			mLabel.setText("手指上滑，取消发送");
		}
	}
	
	/**
	 * 想要取消录音的对话框
	 */
	public void wantToCancel(){
		if(mDialog != null && mDialog.isShowing()){
			mIcon.setVisibility(View.VISIBLE);
			mVoice.setVisibility(View.GONE);
			mLabel.setVisibility(View.VISIBLE);
			
			mIcon.setImageResource(R.drawable.cancel);
			mLabel.setText("松开手指，取消发送");
		}
	}
	
	/**
	 * 时间过短的对话框
	 */
	public void tooShort(){
		if(mDialog != null && mDialog.isShowing()){
			mIcon.setVisibility(View.VISIBLE);
			mVoice.setVisibility(View.GONE);
			mLabel.setVisibility(View.VISIBLE);
			
			mIcon.setImageResource(R.drawable.voice_to_short);
			mLabel.setText("录音时间过短");
		}
	}
	
	/**
	 * 取消对话框
	 */
	public void dimissDialog(){
		if(mDialog != null && mDialog.isShowing()){
			mDialog.dismiss();
			mDialog = null;
		}
	}
	
	/**
	 * 通过level去更新voice上的图片
	 * @param level 1—7
	 */
	public void updateVoiceLevel(int level){
		if(mDialog != null && mDialog.isShowing()){
//			mIcon.setVisibility(View.VISIBLE);
//			mVoice.setVisibility(View.VISIBLE);
//			mLabel.setVisibility(View.VISIBLE);
			
			//通过文件名获得文件的id，我的文件名是 v1~v7
			int resId = mContext.getResources().getIdentifier("v"+level, "drawable", mContext.getPackageName());
			mVoice.setImageResource(resId);
		}
	}
	
	

}
